/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJDialog.java
 *
 * Created on 30/05/2009, 01:46:46 AM
 */
package view;

import bean.Proceso;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

/**
 *
 * @author zAxA
 */
public class DialogoAgregarProcesoArchivo extends javax.swing.JDialog {

    private MenuAplicacionServidor parent;

    /** Creates new form NewJDialog */
    public DialogoAgregarProcesoArchivo(MenuAplicacionServidor parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jfProceso = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jfProceso.setFileFilter(null);
        jfProceso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jfProcesoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jfProceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jfProceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jfProcesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jfProcesoActionPerformed
        // TODO add your handling code here:

        BufferedReader in = null;

        try {
            File data = jfProceso.getSelectedFile();
            in = new BufferedReader(new FileReader(data));

            while (in.ready()) {

                String stream = in.readLine();
                StringTokenizer tok = new StringTokenizer(stream, ",");

                if (tok.countTokens() == 5) {
                    Proceso line = new Proceso();
                    line.setId(tok.nextToken());
                    line.setNombre(tok.nextToken());
                    line.setTipo(tok.nextToken());
                    line.setPrioridad(Byte.valueOf(tok.nextToken()));
                    line.setUsuario(tok.nextToken());
                    parent.getControlProceso().agregarProceso(line);
                }
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
            }
        }
    JOptionPane.showMessageDialog(this, "Se ingresaron los proceso", "Confirmación", JOptionPane.YES_OPTION);

    this.setVisible(false);

}//GEN-LAST:event_jfProcesoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jfProceso;
    // End of variables declaration//GEN-END:variables
}
